idf_component_register(
    SRCS "main.c"
    INCLUDE_DIRS ".")

if($ENV{BUILD_UPDATE_FILE})
    message(STATUS "Build ALL file ${FLASH_ALL_IN_PROJECT}")
    set(FLASH_ALL_IN_PROJECT 1)
else()
    message(STATUS "Build normal file")
    set(FLASH_ALL_IN_PROJECT 0)
endif()

# 定义一个宏来计算文件的MD5哈希
macro(compute_file_md5 filepath md5_var)
    execute_process(
        COMMAND ${CMAKE_COMMAND} -E md5sum ${filepath}
        OUTPUT_VARIABLE ${md5_var}
        OUTPUT_STRIP_TRAILING_WHITESPACE
    )
endmacro()

set(WEB_SRC_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../www")
# 使用宏计算指定文件的MD5
compute_file_md5("${WEB_SRC_DIR}/index.html.gz" FILE_MD5_HAVE_PATH)
# 使用字符串处理去除路径
string(REGEX REPLACE "^([a-f0-9]+).*" "\\1" FILE_MD5 ${FILE_MD5_HAVE_PATH})
# 打印MD5值
message(STATUS "MD5 for index.html.gz: ${FILE_MD5}")
# 添加到全局宏定义
add_definitions(-DWEB_INDEX_FILE_MD5="${FILE_MD5}")

#if(1)
file(READ "${project_dir}/www/cache.html" CACHE_HTML_CONTENTS)
string(REPLACE "FILE_MD5_CONTENTS" "${FILE_MD5}" CACHE_HTML_CONTENTS "${CACHE_HTML_CONTENTS}")
file(WRITE "${build_dir}/cache.html" "${CACHE_HTML_CONTENTS}")
#endif()

if(0 OR FLASH_ALL_IN_PROJECT)
    # fatfs_create_spiflash_image(www ${WEB_SRC_DIR} FLASH_IN_PROJECT)
    littlefs_create_partition_image(www ../www FLASH_IN_PROJECT)
endif()
